package integration.data.task.api.transformation.impl;

import integration.data.task.api.transformation.ComplexTransformer;
import integration.data.task.api.transformation.Transformer;
import integration.modle.exception.IntegrationValidException;

import java.util.HashMap;
import java.util.Map;

public class TransformerRegistry {

    private static Map<String, TransformerInfo> registedTransformer = new HashMap<String, TransformerInfo>();

    static {
        /**
         * add native transformer
         */

        registTransformer(new DataFilter());
        registTransformer(new DataReplace());
        registTransformer(new DataMasking());
    }

    public static synchronized void registTransformer(Transformer transformer) {
        registTransformer(transformer, null, true);
    }

    public static synchronized void registTransformer(Transformer transformer, ClassLoader classLoader, boolean isNative) {


        if (registedTransformer.containsKey(transformer.getTransformerName())) {
            throw new IntegrationValidException("转换函数存在：" + " name=" + transformer.getTransformerName());
        }

        registedTransformer.put(transformer.getTransformerName(), buildTransformerInfo(new ComplexTransformerProxy(transformer), isNative, classLoader));
    }


    private static TransformerInfo buildTransformerInfo(ComplexTransformer complexTransformer, boolean isNative, ClassLoader classLoader) {
        TransformerInfo transformerInfo = new TransformerInfo();
        transformerInfo.setClassLoader(classLoader);
        transformerInfo.setIsNative(isNative);
        transformerInfo.setTransformer(complexTransformer);
        return transformerInfo;
    }


    public static TransformerInfo getTransformer(String transformerName) {
        return registedTransformer.get(transformerName);
    }
}
